<?php
/**
 * 商品属性
 * User: yuanxuncheng
 * Date: 2016-12-21
 * Time: 22:39
 */

namespace app\api\model\dao;

use think\Model;
use think\Db;

class GoodsAttr extends Model
{
    //set talbename
    protected $table = 'goods_attr';
    //set primary key
    protected $pk = 'goods_attr_id';

    /**
     * @desc 增加商品属性
     * @param $goods_id
     * @param $attr_value
     * @param $attr_price
     * @return false|int
     */
    public function addGoodsAttr($param)
    {
        $this->allowField(true)->save($param);
        return $this->data[$this->pk];
    }

    public function updateGoodsAttr($goods_attr_id, $param)
    {
        $conds = array(
            'goods_attr_id' => $goods_attr_id,
        );
        $field = array();
        if (isset($param['attr_value'])) {
            $field['attr_value'] = $param['attr_value'];
        }

        if (isset($param['attr_price'])) {
            $field['attr_price'] = $param['attr_price'];
        }

        return $this->isUpdate(true)->allowField(true)->save($field, $conds);
    }

    public function deleteGoodsAttr($goods_attr_id)
    {
        $conds = array(
            'goods_attr_id' => $goods_attr_id,
        );
        return Db::table($this->table)->where($conds)->delete();
    }

    /**
     * @desc 获取属性
     * @param $goods_id
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function getGoodsAttr($goods_id)
    {
        $conds = array(
            'goods_id' => $goods_id,
        );
        $field = '*';
        return Db::table($this->table)->where($conds)->field($field)->select();
    }
}
